package elitech.vietnam.myfashion.view;

import java.io.IOException;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.huewu.pla.lib.MultiColumnListView;

import elitech.vietnam.myfashion.MainActivity;
import elitech.vietnam.myfashion.R;
import elitech.vietnam.myfashion.doinbackground.CallServiceTask;
import elitech.vietnam.myfashion.entites.Category;
import elitech.vietnam.myfashion.extend.GridCategoriesArrayAdapter;

public class CategoriesView extends LinearLayout {

	private MainActivity mContext;
	private MultiColumnListView mGridView;
	private GridCategoriesArrayAdapter mAdapter;
	private LinearLayout mMainMenu;

	public CategoriesView(MainActivity context) {
		super(context);
		this.mContext = context;
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		if (inflater != null) {
			inflater.inflate(R.layout.categories, this);
		}

		mGridView = (MultiColumnListView) this.findViewById(R.id.cate_grid);
		mMainMenu = (LinearLayout) findViewById(R.id.main_menu);
		mMainMenu.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				mContext.callMenu();
			}
		});

		mAdapter = new GridCategoriesArrayAdapter(mContext, mGridView.getId(), 0, mContext.mCategories);
		mGridView.setAdapter(mAdapter);

		if (mContext.mCategories.size() == 0)
			GetCategories();
	}

	public void GetCategories() {

		new CallServiceTask() {
			ArrayList<Category> rs;

			public void work() {
				try {
					rs = service.getCategory();
				} catch (IOException e) {
					e.printStackTrace();
				} catch (XmlPullParserException e) {}
			};

			protected void onPostExecute(Void result) {
				if (rs != null) {
					mContext.mCategories.clear();
					mContext.mFullCategories.clear();
					mContext.mFullCategories.addAll(rs);
					for (Category category : rs) {
						if (category.ParentID == 0 && category.TrademarksID == 0)
							mContext.mCategories.add(category);
						else if (category.TrademarksID != 0 && category.ParentID == 0) {
							if (checkSubCategory(category)) {
								Category temp = new Category();
								temp.isTrademark = true;
								temp.TrademarksName = category.TrademarksName;
								temp.TrademarksID = category.TrademarksID;
								temp.TrademarksImage = category.TrademarksImage;
								temp.subCategories.add(category);
								mContext.mCategories.add(temp);
							}
						}
					}
					mAdapter.notifyDataSetChanged();
				}

			};
		}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
	}

	public boolean checkSubCategory(Category inCategory) {
		for (Category category : mContext.mCategories) {
			if (category.TrademarksID != 0) {
				if (category.TrademarksID == inCategory.TrademarksID) {
					category.subCategories.add(inCategory);
					return false;
				}
			}
		}
		return true;
	}

	public Category GetCategory(int Id) {
		for (Category category : mContext.mCategories) {
			if (category.Id == Id)
				return category;
		}
		return new Category();
	}

}
